package com.cis467.mrball;

import com.cis467.mrball.parts.Ball;
import com.cis467.mrball.parts.Floor;
import com.cis467.mrball.parts.Level;

import android.content.Context;
import android.graphics.Canvas;

public class MrBallScreenManager extends ScreenManager {

	// Formatting constants
	private static final int MENU_BUFFER = 2;

	// Menu size constants
	private static final int NUM_BUTTONS = 3;

	private static final int NUM_PANEL_SLOTS = 7;

	private int mBallRadius;

	private int mButtonHeight;

	private int mTextItemHeight;
	private int mTextWidthOffset;
	private int mTextHeightOffset;
	
	private int mButtonTextHeightOffset;
	private int mButtonTextWidthOffset;

	public MrBallScreenManager (Context context) {

		super(context, false);
	}

	public void setDimensions(int width, int height) {

		super.setDimensions(width, height);

		// Save the screen size of the ball
		mBallRadius = (int) (mTileSize * (Ball.RADIUS/Floor.SIZE));

		mTextItemHeight = (mScreenHeight - (mTileSize + 4*MENU_BUFFER)) / NUM_PANEL_SLOTS;
		mTextWidthOffset = mSidePanelWidth / 10;
		mTextHeightOffset = mTextItemHeight/2 + SharedConstants.NORMAL_TEXT_SIZE/3;
		
		mButtonHeight = mScreenHeight / NUM_BUTTONS;
		
		mButtonTextHeightOffset = mButtonHeight/2 + SharedConstants.NORMAL_TEXT_SIZE/3;
		mButtonTextWidthOffset = mSidePanelWidth/10;
	}

	public void prerenderLevel(Level pLevel) {
		
		mImageManager.renderLevel(pLevel);
	}
	
	/**
	 * Draws the entire screen
	 */
	public void draw(Canvas canvas, Level pLevel, Ball pBall, String mElapsedTime) {

		this.canvas = canvas;

		clearCanvas();

		drawStatic();

		drawVisibleDynamic(pLevel);

		drawBall(pBall);

		drawSidePanel(mElapsedTime); 

	}

	/**
	 * Draws the ball
	 */
	 private void drawBall(Ball pBall) {

		mImageManager.ball.setBounds((int)((pBall.xPosition * mTileSize) - mBallRadius + mOffsetWidth),
				(int)((pBall.yPosition * mTileSize) - mBallRadius + mOffsetHeight), 
				(int)((pBall.xPosition * mTileSize) + mBallRadius + mOffsetWidth),
				(int)((pBall.yPosition * mTileSize) + mBallRadius + mOffsetHeight));
		mImageManager.ball.draw(canvas);
	 }

	 /**
	  * Draws the sidepanel
	  */
	 private void drawSidePanel(String elapsedTime) {

		 // Draw the side panel
		 canvas.drawRect(mSidePanelOffset, 0, 
				 mScreenWidth, mScreenHeight, 
				 mPaintManager.getPaint(PaintManager.PANEL));

		 // Draw the time label
		 canvas.drawText("Time", 
				 mSidePanelOffset + mTextWidthOffset, 
				 mTextHeightOffset, 
				 mPaintManager.getPaint(PaintManager.TEXT_LARGE));

		 // Draw the time
		 canvas.drawText(elapsedTime, 
				 mSidePanelOffset + mTextWidthOffset, 
				 mTextHeightOffset + mTextItemHeight, 
				 mPaintManager.getPaint(PaintManager.TEXT_LARGE));
	 }

	 public void drawWinScreen(Canvas canvas, String elapsedTime) {

		 drawInBetweenScreen(canvas, "Time: " + elapsedTime);
	 }

	 public void drawGameOverScreen(Canvas canvas) {

		 drawInBetweenScreen(canvas, "Good Job!");
	 }

	 public void drawInBetweenScreen(Canvas canvas, String message) {

		 clearCanvas();

		 canvas.drawBitmap(mImageManager.inBetweenBall, 20, 50, mPaintManager.getPaint(PaintManager.PANEL));
		 
		 // Draw the time label
		 canvas.drawText(message, 
				 mTextWidthOffset, 
				 mTextHeightOffset, 
				 mPaintManager.getPaint(PaintManager.TEXT_LARGE));

		// Draw the buttons
			for(int i = 0; i < NUM_BUTTONS; i++)
				canvas.drawRect(mSidePanelOffset + MENU_BUFFER, 
						mBannerHeight + i*mButtonHeight + MENU_BUFFER, 
						mScreenWidth - MENU_BUFFER, 
						mBannerHeight + (i+1)*mButtonHeight - MENU_BUFFER, 
						mPaintManager.getPaint(PaintManager.CONTAINER));
		 
		 // Draw the Previous label
		 canvas.drawText("Continue", 
				 mSidePanelOffset + mButtonTextWidthOffset + MENU_BUFFER, 
				 mButtonTextHeightOffset + MENU_BUFFER , 
				 mPaintManager.getPaint(PaintManager.TEXT_NORMAL));

		 // Draw the Next label
		 canvas.drawText("Restart", 
				 mSidePanelOffset + mButtonTextWidthOffset + MENU_BUFFER, 
				 mButtonHeight + mButtonTextHeightOffset + MENU_BUFFER , 
				 mPaintManager.getPaint(PaintManager.TEXT_NORMAL));

		 // Draw the Play label
		 canvas.drawText("Exit", 
				 mSidePanelOffset + mButtonTextWidthOffset + MENU_BUFFER, 
				 2*mButtonHeight + mButtonTextHeightOffset + MENU_BUFFER , 
				 mPaintManager.getPaint(PaintManager.TEXT_NORMAL));
	 }

	 public boolean isSideMenuTouch(int x) {

		 return x >= mSidePanelOffset;
	 }

	 public boolean isContinueButton(int y) {

		 return y >= mBannerHeight && y < mBannerHeight + mButtonHeight;
	 }

	 public boolean isRestartButton(int y) {

		 return y >= mBannerHeight + mButtonHeight && y < mBannerHeight + 2*mButtonHeight;
	 }

	 public boolean isExitButton(int y) {

		 return y >= mBannerHeight + 2*mButtonHeight;
	 }

}
